package org.example.demo3.controller;


import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.demo3.enity.ProductDTO;
import org.example.demo3.service.ProductService;
import org.example.demo3.vo.productVO;
import org.example.demo3.vo.result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author xusen
 * @since 2025-03-13
 */
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService iProductService;

    /**
     * 发布商品
     * @param productDTO  前端对象
     * @return  返回成功
     */
    @PostMapping
    public result saveProducts(@Valid ProductDTO productDTO){

        boolean isSaved=iProductService.saveProducts(productDTO);
        if(!isSaved){
            return result.fail("发布失败");
        }
        return result.success("发布成功");
    }

    /**
     * 获取商品详情
     * @param id  前端传入
     * @return   返回对象
     */
    @GetMapping("/{id}")
    public  result<productVO> getProducts(@PathVariable Integer id){
        productVO productVo=iProductService.getProducts(id);
        return result.success(productVo);
    }


    /**
     * 删除商品
     * @param ids  删除商品的id
     * @return     删除成功
     */
    @DeleteMapping("delete")
    public  result deleteProducts(@RequestParam List<String> ids){
        iProductService.removeByIds(ids);
        return result.success("删除商品成功");
    }

}
